/***********************************************************************
** Copyright (C) 2011 Anthony Naddeo <anthony.naddeo@gmail.com>
**
** This file is part of qtweather-qml
**
** qtweather-qml is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**
** qtweather-qml is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this package; If not, see <http://www.gnu.org/licenses/>.
**
***********************************************************************/

import QtQuick 1.1
import com.meego 1.0


Sheet {
	id: root
	acceptButtonText: "Done"

	signal selected(string zipcode)
	signal update(string zipcode)

	content: Flickable {
		anchors.fill: parent
		anchors.leftMargin: 10
		anchors.topMargin: 10
		contentWidth: col.width
		contentHeight: col.height
		flickableDirection: Flickable.VerticalFlick

		Column {
			id: col
			anchors.top: parent.top
			width: parent.width
			spacing: 10
			Repeater{
				id: repeater
				model: 5
				signal check(int ind)
				Item{
					width: root.width
					height: button.height
					Button{
						id: button;
						text: Utilities.home(index+1) !== "" ? (Utilities.home(Utilities.home(index+1)) !== "" ? Utilities.home(Utilities.home(index+1)) : Utilities.home(index+1)) : "Home " + (index+1)
						width: 200
						anchors.left: parent.left
						onClicked: {root.selected(textField.text); root.accept()}
						Connections{
							target: root
							onUpdate: button.text = Utilities.home(index+1) !== "" ? (Utilities.home(Utilities.home(index+1)) !== "" ? Utilities.home(Utilities.home(index+1)) : Utilities.home(index+1)) : "Home " + (index+1)
						}

					}
					TextField{
						id: textField
						validator: RegExpValidator{regExp: /[0-9][0-9][0-9][0-9][0-9]/}
						//						inputMask: "99999"
						placeholderText: "zipcode";
						text: Utilities.home(index+1)
						anchors.left:  button.right;
						anchors.right: parent.right;
						anchors.rightMargin: checkbox.width
						anchors.margins: 5
						onTextChanged: Utilities.setHome((index+1),text)
						inputMethodHints: Qt.ImhDigitsOnly | Qt.ImhNoPredictiveText

					}
					RadioButton{
						id: checkbox
						anchors.right: parent.right
						anchors.verticalCenter: parent.verticalCenter
						checked: (index+1) === Utilities.defaultHome() ? true : false
						onClicked: {
							if(checkbox.checked)
								Utilities.setDefaultHome(index+1)
							else
								Utilities.setDefaultHome(0)
							repeater.check(index)

						}
						Connections{
							target: repeater
							onCheck:{
								if(ind !== index)
									checkbox.checked = false
							}
						}
					}
				}
			}



		}
	}

}
